Passed
Push — development ( 811359...8d1f71 )
by Vad
12:40 queued 13s
created

TravelController.endActiveBikeTravel   A

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 13
dl 0
loc 16
ccs 2
cts 2
cp 1
crap 1
rs 9.75
c 0
b 0
f 0
1 8
import { Controller, Get, Param, Post, Body, UseGuards, Req } from '@nestjs/common';
0 ignored issues
show
introduced by
'Body' is defined but never used.
Loading history...
2 8
import { ApiOperation, ApiResponse, ApiBearerAuth, ApiParam, ApiTags } from '@nestjs/swagger';
3 8
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
4 8
import { TravelService } from './travel.service';
5 8
import { StartRentingDto, TravelResponseDto, EndTravelDto } from './dto/renting.dto';
0 ignored issues
show
introduced by
'StartRentingDto' is defined but never used.
Loading history...
introduced by
'EndTravelDto' is defined but never used.
Loading history...
6
import { Travel } from './entities/travel.entity';
0 ignored issues
show
introduced by
'Travel' is defined but never used.
Loading history...
7
8
@ApiTags('Bike Rentals')
9
@Controller({ path: 'rental', version: '1' })
10 8
export class TravelController {
11 12
  constructor(private readonly travelService: TravelService) {}
12
13
  @Get('customer/:customerId')
14
  // @UseGuards(JwtAuthGuard)
15
  // @ApiBearerAuth()
16
  @ApiOperation({
17
    summary: 'Get all travels for a customer',
18
    description: 'Returns all travels for a specific customer',
19
  })
20
  @ApiResponse({
21
    status: 200,
22
    description: 'Travels found',
23
    type: [TravelResponseDto],
24
  })
25
  @ApiResponse({
26
    status: 404,
27
    description: 'No travels found for this customer',
28
  })
29 8
  async getTravelsForCustomer(@Param('customerId') customerId: string) {
30 2
    return await this.travelService.findTravelsForCustomer(customerId);
31
  }
32
33
  @Get('bike/:bikeId/active')
34
  @ApiOperation({
35
    summary: 'Get active travel for a bike',
36
    description:
37
      'Returns the active travel information including renter details for a specific bike',
38
  })
39
  @ApiResponse({
40
    status: 200,
41
    description: 'Active travel found',
42
    type: TravelResponseDto,
43
  })
44
  @ApiResponse({
45
    status: 404,
46
    description: 'No active travel found for this bike',
47
  })
48 8
  async getActiveTravelForBike(@Param('bikeId') bikeId: string) {
49 3
    return await this.travelService.findActiveTravelForBike(bikeId);
50
  }
51
52
  @Post('bike/:bikeId/end-active')
53
  @ApiOperation({
54
    summary: 'End active travel for specific bike',
55
    description: 'Ends the currently active travel for the specified bike',
56
  })
57
  @ApiResponse({
58
    status: 201,
59
    description: 'Travel ended successfully',
60
  })
61
  @ApiResponse({
62
    status: 404,
63
    description: 'No active travel found for this bike',
64
  })
65 8
  async endActiveBikeTravel(@Param('bikeId') bikeId: string) {
66 2
    return await this.travelService.endActiveTravelForBike(bikeId);
67
  }
68
69
  @Post(':githubId/end-all-travels')
70
  @UseGuards(JwtAuthGuard)
71
  @ApiBearerAuth()
72
  @ApiOperation({
73
    summary: 'End all active travels for a customer',
74
    description: 'Ends all active travels for the specified customer by GitHub ID.',
75
  })
76
  @ApiParam({
77
    name: 'githubId',
78
    description: 'The GitHub ID of the user whose travels are to be ended.',
79
    example: '12345',
80
  })
81
  @ApiResponse({
82
    status: 200,
83
    description: 'All active travels for the customer have been ended.',
84
  })
85
  @ApiResponse({
86
    status: 404,
87
    description: 'Customer or active travels not found.',
88
  })
89 8
  async endAllTravelsForCustomer(@Param('githubId') githubId: string) {
90 2
    return await this.travelService.endAllTravelsForCustomer(githubId);
91
  }
92
93
  // Start a bike rental
94
  @Post('bike/:id')
95
  @UseGuards(JwtAuthGuard)
96
  @ApiBearerAuth()
97
  @ApiOperation({
98
    summary: 'Start renting a bike',
99
    description:
100
      'Initiates a bike travel for the authenticated user. The bike must be available for travel.',
101
  })
102
  @ApiResponse({
103
    status: 201,
104
    description: 'Bike travel started successfully',
105
    type: TravelResponseDto,
106
  })
107
  @ApiResponse({
108
    status: 400,
109
    description: 'Bad Request - Bike not found or not available for renting',
110
  })
111
  @ApiResponse({
112
    status: 401,
113
    description: 'Unauthorized - User not authenticated',
114
  })
115 8
  async startRentingBike(@Param('id') id: string, @Req() req: any) {
116 4
    const userId = req.user.githubId;
117 4
    return this.travelService.startRentingBike(id, userId);
118
  }
119
120
  // End a bike travel
121
  @Post(':id/end')
122
  // removing the guard for now, the bike software should be able to end a travel.
123
  // we could implement a guard that authenticates the bike somehow
124
  // we could also expand on this, making it even more complex, like checking that it is the user whom initlized the rental that is ending it
125
  // @UseGuards(JwtAuthGuard)
126
  @ApiOperation({
127
    summary: 'End a bike travel',
128
    description: 'Ends the travel, calculates cost, and makes the bike available again',
129
  })
130
  @ApiResponse({
131
    status: 201,
132
    description: 'Travel ended successfully',
133
  })
134
  @ApiResponse({
135
    status: 400,
136
    description: 'Bad Request - Travel not found or already ended',
137
  })
138
  @ApiResponse({
139
    status: 401,
140
    description: 'Unauthorized',
141
  })
142 8
  async endTravel(@Param('id') travelId: number) {
143 1
    return this.travelService.endTravel(travelId);
144
  }
145
146
  // Fetch one travel by ID
147
  @Get(':id')
148
  @ApiResponse({
149
    status: 200,
150
    description: 'Travel found',
151
    type: TravelResponseDto,
152
  })
153 8
  async getTravelById(@Param('id') id: number) {
154 2
    return await this.travelService.findById(id);
155
  }
156
157
  // Fetch all travels
158
  @Get()
159 8
  async getAllTravels() {
160 2
    return await this.travelService.findAll();
161
  }
162
}
163